/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ShipinvoiceAddEditPnl.java
 *
 * Created on Jul 24, 2011, 12:18:47 AM
 */
package com.viweb.storemanager.ui.shipping;

import com.viweb.storemanager.core.CursorManager;
import com.viweb.storemanager.core.OptionPane;
import com.viweb.storemanager.dao.Biz;
import com.viweb.storemanager.entity.ShippingInvoice;
import javax.swing.JOptionPane;

/**
 *
 * @author qhvic
 */
public class ShipinvoiceAddEditPnl extends javax.swing.JPanel {

    private ShipinvoicePnl parent;
    private ShippingInvoice shippingInvoice;

    /** Creates new form ShipinvoiceAddEditPnl */
    public ShipinvoiceAddEditPnl(ShipinvoicePnl parent) {
        initComponents();
        this.parent = parent;
    }

    public void assignShippingInvoice(ShippingInvoice shippingInvoice) {
        this.shippingInvoice = shippingInvoice;

        if (this.shippingInvoice.getId() != -1) {
            this.txtVendor.setText(shippingInvoice.getVendor().getName().toString());
            this.txtNote.setText(shippingInvoice.getNote());
            this.dtcDate.setDate(shippingInvoice.getDate());
            this.lbldatemodi.setText(shippingInvoice.getDatemodifies().toString());
            if (shippingInvoice.getStatus() != 0) {
                this.radioYes.setSelected(true);
            } else {
                this.radioNo.setSelected(true);
            }

            if (shippingInvoice.isIsBillable()) {
                this.rdbReceived.setSelected(true);
            } else {
                this.rdbNotreceived.setSelected(true);
            }
        } else {

            this.txtVendor.setText(null);
            this.txtNote.setText(null);
            this.radioYes.setSelected(true);
            this.rdbNotreceived.setSelected(true);
            this.lbldatemodi.setText(null);
        }
    }

    public void setEditableMode(boolean editable) {
        if (editable) {
            this.txtVendor.requestFocus();
        }
        this.txtNote.setEditable(editable);
        this.btnSave.setEnabled(editable);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnGroup1 = new javax.swing.ButtonGroup();
        btnGroupStatus = new javax.swing.ButtonGroup();
        lblVendor = new javax.swing.JLabel();
        lblDate = new javax.swing.JLabel();
        lblNote = new javax.swing.JLabel();
        lblDatemodifier = new javax.swing.JLabel();
        lblIsbillable = new javax.swing.JLabel();
        txtVendor = new javax.swing.JTextField();
        dtcDate = new com.toedter.calendar.JDateChooser();
        scpNote = new javax.swing.JScrollPane();
        txtNote = new javax.swing.JTextPane();
        lbldatemodi = new javax.swing.JLabel();
        rdbReceived = new javax.swing.JRadioButton();
        rdbNotreceived = new javax.swing.JRadioButton();
        btnSave = new javax.swing.JButton();
        lblEmpty = new javax.swing.JLabel();
        btnBrowse = new javax.swing.JButton();
        lblStatus = new javax.swing.JLabel();
        radioYes = new javax.swing.JRadioButton();
        radioNo = new javax.swing.JRadioButton();

        setLayout(new java.awt.GridBagLayout());

        lblVendor.setText("Nhà cung cấp:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 0);
        add(lblVendor, gridBagConstraints);

        lblDate.setText("Ngày nhập:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
        add(lblDate, gridBagConstraints);

        lblNote.setText("Ghi chú:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
        add(lblNote, gridBagConstraints);

        lblDatemodifier.setText("Ngày cập nhật:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        add(lblDatemodifier, gridBagConstraints);

        lblIsbillable.setText("Hóa đơn:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 5, 0);
        add(lblIsbillable, gridBagConstraints);

        txtVendor.setMinimumSize(new java.awt.Dimension(200, 25));
        txtVendor.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 0);
        add(txtVendor, gridBagConstraints);

        dtcDate.setDateFormatString("d MMM, yyyy");
        dtcDate.setMinimumSize(new java.awt.Dimension(200, 25));
        dtcDate.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        add(dtcDate, gridBagConstraints);

        scpNote.setViewportView(txtNote);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        add(scpNote, gridBagConstraints);

        lbldatemodi.setMaximumSize(new java.awt.Dimension(200, 14));
        lbldatemodi.setMinimumSize(new java.awt.Dimension(200, 14));
        lbldatemodi.setPreferredSize(new java.awt.Dimension(200, 14));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        add(lbldatemodi, gridBagConstraints);

        btnGroup1.add(rdbReceived);
        rdbReceived.setText("Đã nhận");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        add(rdbReceived, gridBagConstraints);

        btnGroup1.add(rdbNotreceived);
        rdbNotreceived.setText("Chưa nhận");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        add(rdbNotreceived, gridBagConstraints);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/save.png"))); // NOI18N
        btnSave.setText("Lưu");
        btnSave.setMaximumSize(new java.awt.Dimension(82, 25));
        btnSave.setMinimumSize(new java.awt.Dimension(82, 25));
        btnSave.setPreferredSize(new java.awt.Dimension(82, 25));
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        add(btnSave, gridBagConstraints);

        lblEmpty.setMaximumSize(new java.awt.Dimension(5, 50));
        lblEmpty.setMinimumSize(new java.awt.Dimension(5, 50));
        lblEmpty.setPreferredSize(new java.awt.Dimension(5, 50));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lblEmpty, gridBagConstraints);

        btnBrowse.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/selection.png"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 0);
        add(btnBrowse, gridBagConstraints);

        lblStatus.setText("Tình trạng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 5, 0);
        add(lblStatus, gridBagConstraints);

        btnGroupStatus.add(radioYes);
        radioYes.setText("Có");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
        add(radioYes, gridBagConstraints);

        btnGroupStatus.add(radioNo);
        radioNo.setText("Không");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
        add(radioNo, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            //this.customer.setType((DrinkType) this.cmbDrinkType.getSelectedItem());
            this.shippingInvoice.setNote(this.txtNote.getText().trim());

            boolean status = false;
            if (this.shippingInvoice.getId() != -1) {
                status = Biz.getInstance().insertShippingInvoice(shippingInvoice);
            } else {
                status = Biz.getInstance().updateShippingInvoice(shippingInvoice);
            }
            // Nếu cập nhật (thêm mới hoặc sữa thông tin) không thành công
            if (status) {
                OptionPane.show(JOptionPane.getFrameForComponent(ShipinvoiceAddEditPnl.this), "Cập nhật không thành công! " + Biz.getInstance().getLastError(), "Thông báo", OptionPane.MsgTYPE_WARNING);
                // Nếu cập nhật thành công
            } else {
                if (this.shippingInvoice.getId() == -1) {
                    // Set id cho quốc gia vừa mới cập nhật (chỉ có ý nghĩa trong trường hợp thêm mới)
                    //this.customer.setId(id);
                    OptionPane.show(JOptionPane.getFrameForComponent(ShipinvoiceAddEditPnl.this), "Thêm mới hoá đơn thành công!", "Thông báo");
                } else {
                    OptionPane.show(JOptionPane.getFrameForComponent(ShipinvoiceAddEditPnl.this), "Cập nhật hoá đơn thành công!", "Thông báo");
                }
                this.parent.updateComponentsUI(this.shippingInvoice);
            }
            CursorManager.setDefaultCursor(this);
        } catch (Exception ex) {
        }
    }//GEN-LAST:event_btnSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowse;
    private javax.swing.ButtonGroup btnGroup1;
    private javax.swing.ButtonGroup btnGroupStatus;
    private javax.swing.JButton btnSave;
    private com.toedter.calendar.JDateChooser dtcDate;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblDatemodifier;
    private javax.swing.JLabel lblEmpty;
    private javax.swing.JLabel lblIsbillable;
    private javax.swing.JLabel lblNote;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblVendor;
    private javax.swing.JLabel lbldatemodi;
    private javax.swing.JRadioButton radioNo;
    private javax.swing.JRadioButton radioYes;
    private javax.swing.JRadioButton rdbNotreceived;
    private javax.swing.JRadioButton rdbReceived;
    private javax.swing.JScrollPane scpNote;
    private javax.swing.JTextPane txtNote;
    private javax.swing.JTextField txtVendor;
    // End of variables declaration//GEN-END:variables
}
